
using System;
using System.Linq;
using Csla;
using Csla.Data;
using HousingInfo.DataLinQ;

namespace HousingInfo.Library
{ 
	public partial class NewsBOList : Csla.BusinessListBase<NewsBOList, NewsBO>
	{
        #region ham lay tat ca danh sach tin
        public static NewsBOList GetAllNewBOList()
        {
            return DataPortal.Fetch<NewsBOList>();
        }
        #endregion
		#region Authorization Rules

		protected static void AddObjectAuthorizationRules()
		{
			//TODO: Define object-level authorization rules of NewsBOList
			//AuthorizationRules.AllowCreate(typeof(NewsBOList), "NewsBOListCreateGroup"));
			//AuthorizationRules.AllowEdit(typeof(NewsBOList), "NewsBOListEditGroup"));
			//AuthorizationRules.AllowDelete(typeof(NewsBOList), "NewsBOListDeleteGroup"));
		}
		#endregion //Authorization Rules

        #region truy cap du lieu cua ham lay tat ca danh sach tin
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch()
        {
            bool cancel = false;
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;

                this.AddRange(
                    from row in mgr.DataContext.News
                    orderby row.CreatedDate descending
                    select NewsBO.GetNewsBO(row.NewsID)
                );

                RaiseListChangedEvents = true;
            }//using
        }
        #endregion

        #region Trang Danh muc

        #region Ham kiem tra tin con ton tai trong danh muc tin tuc tong hop can xoa
        public static bool CheckCategoryInNews(int cateID)
        {
            int count = 0;
            using (var mgr = ContextManager<HousingInfoDataContext>
                    .GetManager(Database.HousingInfoConnection))
            {
                count = mgr.DataContext.News.Count(n => n.CategoryID == cateID);
            }

            return (count > 0);
        }
        #endregion // end kiem tra khi xoa danh muc

        #endregion //Trang Danh muc

        #region Ham lay tin tuc theo danh muc trang tin tuc
        #region khai bao ham
        public static NewsBOList GetNewsBOList_ByID(int id)
        {
            return DataPortal.Fetch<NewsBOList>(new FilterCriteriaNews_ByID(id));
        }
        #endregion

        #region khai bao bien
        private class FilterCriteriaNews_ByID
        {
            public int idcategory;
            public FilterCriteriaNews_ByID(int id)
            {
                this.idcategory = id;
            }
        }
        #endregion

        #region truy cap du lieu
        private void DataPortal_Fetch(FilterCriteriaNews_ByID cretia)
        {
            bool cancel = false;
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;

                this.AddRange(
                    from row in mgr.DataContext.News
                    where row.CategoryID == cretia.idcategory
                    orderby row.CreatedDate descending
                    select NewsBO.GetNewsBO(row.NewsID)
                );

                RaiseListChangedEvents = true;
            }//using
        }
        #endregion
        #endregion

        #region Ham lay tin tuc theo danh muc noi that
        #region khai bao ham
        public static NewsBOList GetNewsBOList_ByNoiThat(int idnews_noithat, int soluong)
        {
            return DataPortal.Fetch<NewsBOList>(new FilterCriteriaNews_ByNoiThat(idnews_noithat, soluong));
        }
        #endregion

        #region khai bao bien
        private class FilterCriteriaNews_ByNoiThat
        {
            public int idcategory;
            public int soluong;
            public FilterCriteriaNews_ByNoiThat(int id,int sl)
            {
                this.idcategory = id;
                this.soluong = sl;
            }
        }
        #endregion

        #region truy cap du lieu
        private void DataPortal_Fetch(FilterCriteriaNews_ByNoiThat cretia)
        {
            bool cancel = false;
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;

                this.AddRange(
                    (from row in mgr.DataContext.News
                    where row.CategoryID == cretia.idcategory && row.IsActive == true
                    orderby row.CreatedDate descending
                    select NewsBO.GetNewsBO(row.NewsID)).Take(cretia.soluong)
                );

                RaiseListChangedEvents = true;
            }//using
        }
        #endregion
        #endregion

        #region Ham lay 10 tin moi nhat duoc doc nhieu nhat
        public static NewsBOList TinDocNhieuNhat(int soluong)
        {
            return DataPortal.Fetch<NewsBOList>(new FilterCriteria_TinDocNhieuNhat(soluong));
        }
        #region khai bao bien
        private class FilterCriteria_TinDocNhieuNhat
        {
            public int sluong;
            public FilterCriteria_TinDocNhieuNhat(int sl)
            {
                this.sluong = sl;
            }
        }
        #endregion
        #region Truy Cap Du Lieu
        private void DataPortal_Fetch(FilterCriteria_TinDocNhieuNhat cretia)
        {
            bool cancel = false;
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;
                if (cretia.sluong == -1)
                {
                    this.AddRange(
                        from row in mgr.DataContext.News
                        where row.IsActive == true && row.CategoryID==4
                        // orderby new Random().Next()
                        orderby row.CreatedDate
                        select NewsBO.GetNewsBO(row.NewsID)
                    );
                }
                else
                {
                    this.AddRange(
                        (from row in mgr.DataContext.News
                         where row.IsActive == true
                         orderby row.ViewCount descending
                         select NewsBO.GetNewsBO(row.NewsID)).Take(cretia.sluong)
                    );
                }
                RaiseListChangedEvents = true;
            }//using
        }
        #endregion
        #endregion

        #region Ham lay tin tuc theo danh muc ngoai trang user( Lay cac tin da active)
        #region khai bao ham
        public static NewsBOList GetID_NewsBOList(int id, int trang, int soluong)
        {
            return DataPortal.Fetch<NewsBOList>(new FilterCriteriaGetIDNewsBOList(id, trang, soluong));
        }
        #endregion

        #region khai bao bien
        private class FilterCriteriaGetIDNewsBOList
        {
            public int idcategory1;
            public int sotrang;
            public int soluongtin;    
            public FilterCriteriaGetIDNewsBOList(int id1, int st, int sl)
            {
                this.idcategory1 = id1;
                this.sotrang = st;
                this.soluongtin = sl;
            }
        }
        #endregion

        #region truy cap du lieu
        private void DataPortal_Fetch(FilterCriteriaGetIDNewsBOList cretia)
        {
            bool cancel = false;
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;
                if (cretia.soluongtin == -1 && cretia.sotrang == -1)
                {
                    this.AddRange(
                        from row in mgr.DataContext.News
                        where row.CategoryID == cretia.idcategory1 && row.IsActive == true
                        orderby row.CreatedDate descending
                        select NewsBO.GetNewsBO(row.NewsID)
                    );
                }
                else
                {
                    this.AddRange(
                        (from row in mgr.DataContext.News
                        where row.CategoryID == cretia.idcategory1 && row.IsActive == true
                        orderby row.CreatedDate descending
                        select NewsBO.GetNewsBO(row.NewsID)).Skip((cretia.sotrang -1)* cretia.soluongtin).Take(cretia.soluongtin)
                    );
                }

                RaiseListChangedEvents = true;
            }//using
        }
        #endregion
        #endregion

        #region Ham Tim kiem tin tuc theo tu khoa
        #region khai bao ham
        public static NewsBOList Timkiemtintuc(string tukhoa, int trang, int soluong)
        {
            return DataPortal.Fetch<NewsBOList>(new FilterCriteriaTimkiemtintuc(tukhoa, trang, soluong));
        }
        #endregion

        #region khai bao bien
        private class FilterCriteriaTimkiemtintuc
        {
            public string tukhoa;
            public int sotrang;
            public int soluongtin;
            public FilterCriteriaTimkiemtintuc(string tk, int st, int sl)
            {
                this.tukhoa = tk;
                this.sotrang = st;
                this.soluongtin = sl;
            }
        }
        #endregion

        #region truy cap du lieu
        private void DataPortal_Fetch(FilterCriteriaTimkiemtintuc cretia)
        {
            bool cancel = false;
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;
                if (cretia.soluongtin == -1 && cretia.sotrang == -1)
                {
                    this.AddRange(
                        from row in mgr.DataContext.News
                        where row.Title.Contains(cretia.tukhoa) && row.IsActive == true
                        orderby row.CreatedDate descending
                        select NewsBO.GetNewsBO(row.NewsID)
                    );
                }
                else
                {
                    this.AddRange(
                        (from row in mgr.DataContext.News
                         where row.Title.Contains(cretia.tukhoa) && row.IsActive == true
                         orderby row.CreatedDate descending
                         select NewsBO.GetNewsBO(row.NewsID)).Skip((cretia.sotrang - 1) * cretia.soluongtin).Take(cretia.soluongtin)
                    );
                }

                RaiseListChangedEvents = true;
            }//using
        }
        #endregion
        #endregion
    }
}
